package cse.om.spellchecker;

public class Checker {

	static final String ie = "i í ì ỉ ĩ ị	e é è ẻ ẽ ẹ	ê ế ề ể ễ ệ";
	static final String o = "o ó ò ỏ õ ọ	ô ố ồ ổ ỗ ộ	ơ ớ ờ ở ỡ ợ";

	public static String[] correct(String[] sentences) {
		String[] result = new String[sentences.length];
		for (int i = 0; i < sentences.length; ++i) {
			result[i] = correctSentence(sentences[i]);
		}
		return result;
	}

	public static String correctSentence(String sentence) {
		char[] c = sentence.toCharArray();
		StringBuilder sb = new StringBuilder();
		StringBuilder word = new StringBuilder();
		for (int i = 0; i < c.length; ++i) {
			if (Dictionary.isVietnameseCharacter(c[i])) {
				word.append(c[i]);
			} else {
				if (word.length() > 0) {
					sb.append(correctWord(word.toString()));
				}
				word.delete(0, word.length());
				sb.append(c[i]);
			}
		}
		if (word.length() > 0) {
			sb.append(correctWord(word.toString()));
		}
		return sb.toString();
	}

	private static String correctWord(String word) {
		word = Dictionary.getDefination(word);
		if (word.length() > 0) {
			switch (word.charAt(0)) {
				case 'c':
				case 'C':
					word = correctFirstC(word);
					break;
				case 'k':
				case 'K':
					word = correctFirstK(word);
					break;
				case 'f':
				case 'F':
					word = correctFirstF(word);
					break;
				case 'w':
				case 'W':
					word = correctFirstW(word);
					break;
				case 'j':
				case 'J':
					word = correctFirstJ(word);
					break;
				case 'd':
				case 'D':
					if (word.length() > 2 && Character.toLowerCase(word.charAt(1)) == 'z') {
						word = correctFirstDZ(word);
					}
					break;
				case 'z':
				case 'Z':
					word = correctFirstZ(word);
					break;
			}
		}
		word = correctJ(word);
		word = correctNG(word);
		return word;
	}

	private static String correctFirstC(String word) {
		if (word.length() >= 2 && ie.indexOf(Character.toLowerCase(word.charAt(1))) >= 0) {
			if (word.charAt(0) == 'c') {
				word = "k" + word.substring(1);
			} else {
				word = "K" + word.substring(1);
			}
		}
		return word;
	}

	private static String correctFirstW(String word) {
		if (word.length() <= 1) return word;
		if (Character.toLowerCase(word.charAt(1)) == 'u') {
			if (word.charAt(0) == 'W') {
				word = 'Q' + word.substring(1);
			} else {
				word = 'q' + word.substring(1);
			}
		} else {
			if (word.charAt(0) == 'W') {
				if (Character.isUpperCase(word.charAt(1))) {
					word = "QU" + word.substring(1);
				} else {
					word = "Qu" + word.substring(1);
				}
			} else {
				word = "qu" + word.substring(1);
			}
		}
		return word;
	}

	private static String correctNG(String word) {
		int n = word.length();
		if (n >= 2 && Dictionary.isVowel(word.charAt(n - 2))) {
			if (word.charAt(n - 1) == 'g') {
				word = word.replaceFirst("g$", "ng");
			} else if (word.charAt(n - 1) == 'G') {
				word = word.replaceFirst("G$", "NG");
			}
		}
		return word;
	}

	private static String correctFirstK(String word) {
		if (word.length() < 2) {
			return word;
		}
		if (o.indexOf(Character.toLowerCase(word.charAt(1))) >= 0) {
			if (word.charAt(0) == 'k') {
				word = "c" + word.substring(1);
			} else {
				word = "C" + word.substring(1);
			}
		}
		return word;
	}

	private static String correctFirstF(String word) {
		if (word.length() < 2) {
			return word;
		}
		if (word.charAt(0) == 'F') {
			if (Character.isUpperCase(word.charAt(1))) {
				word = "PH" + word.substring(1);
			} else {
				word = "Ph" + word.substring(1);
			}
		} else {
			word = "ph" + word.substring(1);
		}
		return word;
	}

	private static String correctFirstZ(String word) {
		if (word.charAt(0) == 'Z') {
			word = 'V' + word.substring(1);
		} else {
			word = 'v' + word.substring(1);
		}
		return word;
	}

	private static String correctFirstDZ(String word) {
		if (word.length() < 3) {
			return word;
		}
		if (word.charAt(0) == 'D') {
			word = 'V' + word.substring(2);
		} else {
			word = 'v' + word.substring(2);
		}
		return word;
	}

	private static String correctFirstJ(String word) {
		if (word.charAt(0) == 'J') {
			if (Character.isUpperCase(word.charAt(1))) {
				word = "GI" + word.substring(1);
			} else {
				word = "Gi" + word.substring(1);
			}
		} else {
			word = "gi" + word.substring(1);
		}
		return word;
	}
	
	private static String correctJ(String word) {
		return word.replaceAll("j", "i");
	}
}
